// pages/community/addComment/addComment.js
const request = require('../../../../api/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    titleNum: 0,
    contentNum: 0,
    avatarUrl: null,
    imageUrls: [],
    goodsList: [],
    count: 5
  },
  // 上传图片
  chooseImage: function () {
    var that = this;
    let count = that.data.count - that.data.imageUrls.length
    if (count > 0) {
      wx.chooseImage({
        count: count, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths
          //启动上传等待中...  
          wx.showToast({
            title: '正在上传...',
            icon: 'loading',
            mask: true,
            duration: 200000
          })
          console.log(tempFilePaths)
          var filePaths = that.data.imageUrls
          tempFilePaths.forEach(ele => {
            wx.uploadFile({
              url: request.BASE_URL + '/dfs/upload', //仅为示例，非真实的接口地址
              filePath: ele,
              name: 'file',
              formData: {
                'user': 'test'
              },
              success: function (res) {
                var data = res.data
                let tempData = JSON.parse(data)
                filePaths.push(tempData.data.url)
                that.setData({
                  //将临时变量赋值给已经在data中定义好的变量
                  avatarUrl: filePaths,
                  imageUrls: filePaths
                })
                wx.hideToast()
              },
              fail: function (e) {
                console.log(e.message)
              }
            })
          });
        }
      })
    } else {
      wx.showToast({
        title: '最多只能上传5张哦~',
        icon: 'none',
        duration: 200000
      })
    }
  },
  handleInput: function (e) {
    this.setData({
      titleNum: e.detail.cursor
    })
  },
  handleTextarea: function (e) {
    this.setData({
      contentNum: e.detail.cursor
    })
  },
  deleteGoods: function (val) {
    const that = this
    const goodId = val.currentTarget.dataset.goodid
    const goodsList = that.data.goodsList
    let goodItem = {}
    goodsList.forEach(item => {
      if (item.id === goodId) {
        goodItem = item
      }
    })
    var index = goodsList.indexOf(goodItem);
    goodsList.splice(index, 1);
    that.setData({
      goodsList: goodsList
    })
  },
  // 添加关联商品
  addGoods: function () {
    wx.navigateTo({
      url: '../../community/addGoods/addGoods'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const self = this
    let tempList = self.data.goodsList
    try {
      var tempGoods = wx.getStorageSync('wj_tempGoods')
      if (tempGoods) {
        tempList.push(tempGoods)
        self.setData({
          goodsList: tempList
        })
        wx.removeStorageSync('wj_tempGoods')
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})